// pages/index/search/searchResult/searchResult.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    travelogueList:[],
   
    // tab选项
    tabList: [{
        title: "全部",
        index: "0",
      },
      {
        title: "酒店",
        index: "1",
      },
      {
        title: "游记",
        index: "2",
      },
      {
        title: "问答",
        index: "3",
      },
      {
        title: "用户",
        index: "4",
      }
    ],
    tabsId: 0, //默认选型
    swiperHeight: '',
    tagList: ['三天两晚自由行', '韩国自由行', '毕业旅行 自由行', '*********************'],
    collectOrNot: 0, // 0未收藏  1已收藏
    value: 3, //评分

    followOrNot: 0, // 0未关注  1已关注
<<<<<<< HEAD
    list: []
=======
    hotelList: [],
    list: [0, 1, 2, 3],
    questionList: '', //问答列表
    userList: '' , //用户列表
    userId: ''

>>>>>>> a0ceacf3cd24088d881266ff089febe706980c13
  },
  // 滑动时触发的事件
  slideOn(e) {
    // 拿到当前索引并动态改变
    this.setData({
      tabsId: e.detail.current
    })
  },

  //点击tab时触发
  tabsOn(e) {
    this.setData({
      //拿到当前索引并动态改变
      tabsId: e.currentTarget.dataset.idx
    })
    // this.getHeight()
  },
  getHeight(str) { //根据不同的类名，去获取不同的内容高度
    // 获取元素高度
    let query = wx.createSelectorQuery();
    //选择id
    let that = this;
    query.select('.' + str).boundingClientRect(function (rect) {
      that.setData({
        swiperHeight: rect.height + 20 + 'px'
      })
    }).exec();
    console.log(this.data.swiperHeight)
  },
  // 喜欢
  changeLike() {
    console.log('collectOrNot', this.data.collectOrNot)
    if (this.data.collectOrNot != 1) {
      // 对接后端数据库发请求...点赞数改变
      this.setData({
        collectOrNot: 1
      })
    } else {
      // 对接后端数据库发请求...点赞数改变
      this.setData({
        collectOrNot: 0
      })
    }
  },
  // 评分
  onChange(event) {
    this.setData({
      value: event.detail,
    });
  },
  // 关注
  changeFollow(e) {
    console.log(e)
    if(e.target.dataset.isfollowed==1){
      var id = getApp().globalData.loginfo.userId;
      wx.request({
        url: 'http://localhost:8080/user/followed/deleteMyFollowed',
        method: 'delete',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
          followedId: id, //登录的用户id
          userId: e.target.dataset.userid,
        },
        success:(resp)=>{
          //重新查询用户中心列表
          this.getUserByNickName(this.data.info)
        }
  
      })
    }else if(e.target.dataset.isfollowed==0) {
      //获取我的id
      var id = getApp().globalData.loginfo.userId;
      wx.request({
        url: 'http://localhost:8080/user/followed/followOther',
        method: 'POST',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
        //因为先查找自己是否被他关注，所以这里的关注人idfollowedId为他的id，userId也就是被关注的人的id写我的id,后面步			骤在业务层已经处理过了
          followedId: e.target.dataset.userid,
          userId: id
        },
        success:(resp)=> {
          //重新查询用户中心列表
          this.getUserByNickName(this.data.info)
        }
      })
    }
    
  },

  getList(){

    wx.request({
      url: 'http://localhost:8080/travelogue/travelogue/list',
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        this.setData({
         list: resp.data.rows
        })
        console.log(this.data.list)
      }
    })
  },

  getScenic: function (params) {
    wx.request({
      url: 'http://localhost:8080/scenic/scenic/list',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        scenicName: params
      },
      success: (resp) => {
        this.setData({
          scenicList: resp.data.rows
        })
        console.log("scenicList", this.data.scenicList)
      }
    })
  },
  // 酒店
  getHotel(text) {
    if (text == '') {
      wx.request({
        url: 'http://localhost:8080/hotel/hotel/list',
        method: 'GET',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
          hotelName: text
        },
        success: (resp) => {
          console.log("haha", resp)
          this.setData({
            hotelList: resp.data.rows,
            //totalPageCount: resp.data.data.pageCount,
          })
        },
      })
    } else {
      wx.request({
        url: 'http://localhost:8080/hotel/hotel/list',
        method: 'GET',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
          hotelName: text
        },
        success: (resp) => {
          console.log("haha", resp)
          this.setData({
            hotelList: resp.data.rows,
            //totalPageCount: resp.data.data.pageCount,
          })
        },
      })
    }
  },

  // 获取问题列表
  getQuestionByTitle(e) {
    if(e!='') {
      wx.request({
        url: 'http://localhost:8080/content/question/questionTitle',
        method: 'get',
        header: {
          Authorization: getApp().globalData.authorization
        },
        data: {
          questionTitle: e//传入标题
        },
        success:(resp)=>{
          this.setData({
            questionList: resp.data.data
          })
        }
      })
    }
    
  },

  //查询用户
  //根据昵称模糊搜索用户
getUserByNickName(e) {
  if(e!='') {
    wx.request({
      url: 'http://localhost:8080/system/user/userByNickName',
      method: 'get',
      header: {
        Authorization: getApp().globalData.authorization
      },
      data: {
        nickName: e,//传入昵称,
        followedId: this.data.userId
      },
      success:(resp)=>{
        console.log('uesrLits', resp.data)
        this.setData({

          userList: resp.data.rows
        })
      }
    })
  }
	
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
<<<<<<< HEAD
    this.getList()
    this.getHeight('allInfo')
    this.getScenic(options.info)
    wx.request({
      url: 'http://localhost:8080/travelogue/travelogue/list',
      method: 'GET',
      header: {
        Authorization: getApp().globalData.authorization
      },
      success: (resp) => {
        console.log(resp.data)
        this.setData({
          travelogueList: resp.data.rows
        })
      }
    })
=======
    console.log('options',options)
    var loginfo= getApp().globalData.loginfo;
    console.log(loginfo)
    this.setData({
      userId: loginfo.userId,
      info: options.info
    })
    this.getHeight('allInfo')
    this.getScenic(options.info)

    this.getHotel(options.info)

    this.getQuestionByTitle(options.info)
    this.getUserByNickName(options.info)

>>>>>>> a0ceacf3cd24088d881266ff089febe706980c13
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getUserByNickName(this.data.info)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})